package cn.iocoder.yudao.module.member.controller.admin.confmaterial;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.member.controller.admin.confmaterial.vo.MemberConfMaterialPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.confmaterial.vo.MemberConfMaterialRespVO;
import cn.iocoder.yudao.module.member.controller.admin.confmaterial.vo.MemberConfMaterialSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.confmaterial.MemberConfMaterialDO;
import cn.iocoder.yudao.module.member.service.memberconf.confmaterial.MemberConfMaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 物料策略配置")
@RestController
@RequestMapping("/member/conf-material")
@Validated
public class MemberConfMaterialController {

    @Resource
    private MemberConfMaterialService confMaterialService;

    @PostMapping("/create")
    @Operation(summary = "创建物料策略配置")
    @PreAuthorize("@ss.hasPermission('member:conf-material:create')")
    public CommonResult<Long> createMemberConfMaterial(@Valid @RequestBody MemberConfMaterialSaveReqVO createReqVO) {
        return success(confMaterialService.createMemberConfMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料策略配置")
    @PreAuthorize("@ss.hasPermission('member:conf-material:update')")
    public CommonResult<Boolean> updateMemberConfMaterial(@Valid @RequestBody MemberConfMaterialSaveReqVO updateReqVO) {
        confMaterialService.updateMemberConfMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料策略配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:conf-material:delete')")
    public CommonResult<Boolean> deleteMemberConfMaterial(@RequestParam("id") Long id) {
        confMaterialService.deleteMemberConfMaterial(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料策略配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:conf-material:query')")
    public CommonResult<MemberConfMaterialRespVO> getMemberConfMaterial(@RequestParam("id") Long id) {
        MemberConfMaterialDO confMaterial = confMaterialService.getMemberConfMaterial(id);
        return success(BeanUtils.toBean(confMaterial, MemberConfMaterialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料策略配置分页")
    @PreAuthorize("@ss.hasPermission('member:conf-material:query')")
    public CommonResult<PageResult<MemberConfMaterialRespVO>> getMemberConfMaterialPage(@Valid MemberConfMaterialPageReqVO pageReqVO) {
        PageResult<MemberConfMaterialDO> pageResult = confMaterialService.getMemberConfMaterialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MemberConfMaterialRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料策略配置 Excel")
    @PreAuthorize("@ss.hasPermission('member:conf-material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMemberConfMaterialExcel(@Valid MemberConfMaterialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MemberConfMaterialDO> list = confMaterialService.getMemberConfMaterialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料策略配置.xls", "数据", MemberConfMaterialRespVO.class,
                        BeanUtils.toBean(list, MemberConfMaterialRespVO.class));
    }

}